<?php

/**
 * The Shell class has the principal methods for running the Design Pattern.
 * 
 * @author Cesar Augustus Silva <cesaraugustussilva@linuxmail.org>
 * @copyright Copyright (c) 2013, Cesar Augustus Silva
 * @license http://www.gnu.org/licenses/gpl.html GNU GPL v3.0
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <­http://www.gnu.org/licenses/>.
 */
class Shell {

    /**
     * Creates a new instance of the controller class whether not instantiated
     * 
     * @param string $class The class name
     * @return class A new instance of the class
     */
    public static function getController($class) {
        if (!class_exists($class, false)) {
            require_once(dirname(__FILE__).'/../controllers/'.$class.'.php');
            $core = new ReflectionClass($class.'Core');
            if ($core->isAbstract())
                eval('abstract class '.$class.' extends '.$class.'Core {}');
            else
                eval('class '.$class.' extends '.$class.'Core {}');
        }
        return new $class();
    }
    
    /**
     * Adds the styles in a array to then be shown
     * 
     * @global string|array $css_files The list of styles
     * @param string|array $uri The uri of styles to be added
     * @param string $media [optional]<br>The media type of style
     * @return void No value is returned
     */
    public static function addStyle($uri, $media = 'all') {
        global $css_files;
        if (is_array($uri)) {
            foreach ($uri as $uri2)
                self::addStyle($uri2, $media);
        }
        else {
            if (file_exists(_CSS_DIR_.$uri.'.css') && !in_array($uri, $css_files)) {
                $uri = array($uri => $media);
                if (is_array($css_files))
                    $css_files = array_merge($css_files, $uri);
                else
                    $css_files = $uri;
            }
        }
    }
    
    /**
     * Adds the scripts in a array to then be shown
     * 
     * @global string|array $js_files The list of scripts
     * @param string|array $uri The uri of scripts to be added
     * @return void No value is returned
     */
    public static function addScript($uri) {
        global $js_files;
        if (is_array($uri)) {
            foreach ($uri as $uri2)
                self::addScript($uri2);
        }
        else {
            if (file_exists(_JS_DIR_.$uri.'.js') && !in_array($uri, $js_files)) {
                $uri = array($uri);
                if (is_array($js_files))
                    $js_files = array_merge($js_files, $uri);
                else
                    $js_files = $uri;
            }
        }
    }
    
    /**
     * Includes certain view page
     * 
     * @param string $filename The name of the view page
     * @return void No value is returned
     */
    public static function showPage($filename) {
        if (file_exists(_PAGE_DIR_.$filename.'.php'))
            include_once(_PAGE_DIR_.$filename.'.php');
    }
    
    /**
     * Adds the widgets in a array to then be shown
     * 
     * @global string|array $widgets The list of widgets
     * @param string|array $name The name of widgets to be added
     * @param type $sidebar The name of the sidebar to be added the widgets
     * @return void No value is returned
     */
    public static function addWidget($name, $sidebar) {
        global $widgets;
        if (!is_array($widgets))
            $widgets = array();
        
        if (is_array($name)) {
            foreach ($name as $name2)
                self::addWidget($name2, $sidebar);
        }
        else {
            if (file_exists(_WIDGET_DIR_.$name.'.php') && !in_array($name, $widgets))
                $widgets[$sidebar][] = $name;
        }
    }
    
    /**
     * Includes certain sidebar widgets
     * 
     * @global array $widgets The widgets
     * @param string $sidebar The name of the sidebar
     * @return void No value is returned
     */
    public static function showWidgets($sidebar) {
        global $widgets;
        if (is_array($widgets) && array_key_exists($sidebar, $widgets)) {
            foreach ($widgets[$sidebar] as $name)
                include_once(_WIDGET_DIR_.$name.'.php');
        }
    }

}